package org.hxyjs.dao.impl;

import org.hxyjs.dao.PraiseChefDao;
import org.hxyjs.entity.PraiseChef;
import org.hxyjs.mapper.PraiseChefMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("praiseChefDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class PraiseChefDaoImpl implements PraiseChefDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private PraiseChefMapper praiseChefMapper;

    @Override
    public List<PraiseChef> getPraiseChefByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) praiseChefMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return praiseChefMapper.getPraiseChefByPage(map);
    }


    public List<PraiseChef> getAllPraiseChefs() throws Exception {
        return praiseChefMapper.selectByExample(null);
    }

    @Override
    public PraiseChef getPraiseChefByUcno(String ucno) throws Exception {
        return praiseChefMapper.selectByPrimaryKey(ucno);
    }


    @Override
    public Integer deletePraiseChefByUcno(String ucno) {
        return praiseChefMapper.deleteByPrimaryKey(ucno);
    }

    @Override
    public Integer updatePraiseChefByUcno(PraiseChef praiseChef) {

        return praiseChefMapper.updateByPrimaryKey(praiseChef);
    }

    @Override
    public Integer addPraiseChef(PraiseChef praiseChef) {
        praiseChef.setUcno(initId());
        return praiseChefMapper.insertSelective(praiseChef);
    }

    public String initId() {
        String maxUcno = praiseChefMapper.getMaxUcno();
        return GenNumTools.initId("1900010009202104301", 5, maxUcno);
    }

}